package com.tianbing.adlottery.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.jubao.ejb.business.ProductBusiness;
import com.jubao.ejb.dao.BrandDao;
import com.jubao.ejb.dao.CategoryDao;
import com.jubao.ejb.dao.ProductDao;
import com.jubao.ejb.entity.Brand;
import com.jubao.ejb.entity.Category;
import com.jubao.ejb.entity.Product;
import com.jubao.ejb.entity.ProductPrice;
import com.tianbing.adlottery.helper.SkinUtils;

/**
 * MultiActionController for the image list/upload UI.
 * 
 * @author Conan Chen
 * @since 2006.12.15
 */
public class ProductController extends MultiActionController {
	Logger logger = Logger.getLogger(ProductController.class.getName());

	private String productDetailView;

	private ProductDao productDao;

	private BrandDao brandDao;

	private CategoryDao categoryDao;

	private SkinUtils skinUtils;

	private ProductBusiness productBusiness;

	@SuppressWarnings("unchecked")
	public ModelAndView viewProductDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(productDetailView);
		Map model = mv.getModel();

		// --get request parameter
		Long prdsid = skinUtils.getParameterProductSysId(request);
		Long pdgsid = ServletRequestUtils.getLongParameter(request, "pdgsid",-1);
		Long argsid = skinUtils.getParameterAreaCategorySysId(request);
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		int pgsize = 20;
		// --/get request parameter

		Product product = productDao.find(prdsid);
		if (product != null) {
			//1. get product info
			if (product.getBrand() != null) {
				Long brdsid = product.getBrand().getSysId();
				Brand brand = brandDao.find(brdsid);
				model.put("brand", brand);
			}
			
			if(pdgsid==-1 && product.getPrimaryCategory()!=null){
				pdgsid = product.getPrimaryCategory().getSysId();
			}
			
			Category productCategory = categoryDao.find(pdgsid);
			//get category primary trail
			List<Category> primaryProductCategoryTrail = null;
			if (productCategory != null) {
				primaryProductCategoryTrail = categoryDao.getPrimaryCategoryTrail(pdgsid);
			}

			//2. get product store info
			Category areaCategory = categoryDao.find(argsid);
			//get category primary trail
			List<Category> areaPrimaryCategoryTrail = null;
			if (areaCategory != null) {
				areaPrimaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(argsid);
			}
			List<Category> areaChildCategoryList = categoryDao.getChildCategoryList(argsid, pgno, pgsize, null);

			List<ProductPrice> productPriceList = productBusiness.getProductPriceList(argsid, -1, prdsid, pgno, pgsize);
			long totalCount = productBusiness.getProductPriceCount(argsid, -1, prdsid);

			model.put("product", product);
			model.put("productCategory", productCategory);
			model.put("primaryProductCategoryTrail", primaryProductCategoryTrail);
			model.put("areaCategory", areaCategory);
			model.put("areaPrimaryCategoryTrail", areaPrimaryCategoryTrail);
			model.put("areaChildCategoryList", areaChildCategoryList);

			model.put("productPriceList", productPriceList);

			int pageItemNum = productPriceList == null ? 0 : productPriceList.size();
			String otherParam = "prdsid=" + prdsid + "&pdgsid=" + pdgsid +  "&argsid=" + argsid;
			model = skinUtils.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pgsize, otherParam,
					model);

		}

		return mv;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setProductDetailView(String productDetailView) {
		this.productDetailView = productDetailView;
	}

	/**
	 * @param productBusiness the productBusiness to set
	 */
	public void setProductBusiness(ProductBusiness productBusiness) {
		this.productBusiness = productBusiness;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setBrandDao(BrandDao brandDao) {
		this.brandDao = brandDao;
	}

}
